package com.xysq.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.FadeExit.FadeExit;
import com.flyco.animation.FlipEnter.FlipVerticalSwingEnter;
import com.flyco.dialog.listener.OnBtnLeftClickL;
import com.flyco.dialog.listener.OnBtnRightClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.rockcent.action.AppAction;
import com.rockcent.action.CallbackListener;
import com.rockcent.model.CustomerBO;
import com.rockcent.model.CustomerCouponBO;
import com.umeng.analytics.MobclickAgent;
import com.xysq.XYApplication;
import com.xysq.activity.PublishGoodsActivity;
import com.xysq.fragment.MyPublishedFragment;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UseUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 描述信息
 *
 * @author keegan
 * @date 15/8/10
 */
public class MyPublishedAdapter extends BaseDataAdapter<CustomerCouponBO> {

    AppAction appAction;
    String type;
    MyPublishedFragment fragment;
    BaseAnimatorSet bas_in = new FlipVerticalSwingEnter();
    BaseAnimatorSet bas_out = new FadeExit();


    public MyPublishedAdapter(MyPublishedFragment fragment, Context context, AppAction appAction, String type) {
        super(context);
        this.fragment = fragment;
        this.appAction = appAction;
        this.type = type;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_list_my_coupon, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final CustomerCouponBO couponBO = itemList.get(position);
        holder.titleTxt.setText(couponBO.getName());
//        holder.priceTxt.setText("￥" + UseUtil.handlePrice(couponBO.getPrice()));
        holder.priceTxt.setText(couponBO.getIntroduce());
        if (type == MyPublishedFragment.TYPE_SALING) {
            holder.rightBtn.setText(context.getResources().getString(R.string.btn_put_off));
        } else {
            holder.rightBtn.setText(context.getResources().getString(R.string.btn_put_on));
        }
        String logoUrl = couponBO.getLogoUrl();
        if (TextUtils.isEmpty(logoUrl)) {
            holder.couponImg.setImageResource(R.drawable.def_img_head);
        } else if (logoUrl.startsWith("http://")) {
            XYApplication.getImageLoader().displayImage(logoUrl, holder.couponImg);
        } else {
            XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + logoUrl, holder.couponImg);
        }
        holder.rewardTv.setText("已被打赏" + couponBO.getSaleCount() + "次");
        holder.middleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == MyPublishedFragment.TYPE_SOLD_OUT) {
                    MobclickAgent.onEvent(context, "1035");
                }
                if (type == MyPublishedFragment.TYPE_SALING) {
                    MobclickAgent.onEvent(context, "1034");
                }
                Intent intent = new Intent(context, PublishGoodsActivity.class);
                intent.putExtra(PublishGoodsActivity.PARAM_CUSTOMER_COUPON, couponBO);
                context.startActivity(intent);
            }
        });
        holder.rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == MyPublishedFragment.TYPE_SALING) {
                    MobclickAgent.onEvent(context, "1037");
                    confirmUnderCarriage(couponBO);
                } else {
                    MobclickAgent.onEvent(context, "1036");
                    confirmGrounding(couponBO);
                }
            }
        });
        getCustomer(String.valueOf(couponBO.getCustomerId()),holder);
        return convertView;
    }

    //查询发券人信息
    private void getCustomer(String customerId, final ViewHolder holder) {
        appAction.getCustomer("", "", customerId, new CallbackListener<CustomerBO>() {
            @Override
            public void onSuccess(CustomerBO data) {
                if (TextUtils.isEmpty(data.getHeadUrl()) || data.getHeadUrl().equals("null")) {
                    holder.picIv.setImageResource(R.drawable.def_img_head);
                } else{
                    XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + data.getHeadUrl(), holder.picIv);
                }
                String name = UseUtil.selectName(data.getNickname(), data.getName(), data.getPhone());
                holder.nameTv.setText(name);
                holder.schoolTv.setText(context.getResources().getString(R.string.txt_from) + data.getSchoolName());
            }

            @Override
            public void onFailure(String errorCode, String message) {
            }
        });
    }

    class ViewHolder {
        @InjectView(R.id.img_coupon)
        ImageView couponImg;
        @InjectView(R.id.txt_title)
        TextView titleTxt;
        @InjectView(R.id.txt_price)
        TextView priceTxt;
        @InjectView(R.id.btn_middle)
        Button middleBtn;
        @InjectView(R.id.btn_right)
        Button rightBtn;
        @InjectView(R.id.tv_name)
        TextView nameTv;
        @InjectView(R.id.iv_pic)
        ImageView picIv;
        @InjectView(R.id.tv_school)
        TextView schoolTv;
        @InjectView(R.id.tv_reward)
        TextView rewardTv;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

    private void confirmGrounding(final CustomerCouponBO couponBO) {
        final NormalDialog dialog = new NormalDialog(context);
        dialog.content("是否确定发布？")
                .contentTextColor(context.getResources().getColor(R.color.text_blue))
                .isTitleShow(false)
                .showAnim(bas_in)
                .dismissAnim(bas_out)
                .show();

        dialog.setOnBtnLeftClickL(new OnBtnLeftClickL() {
            @Override
            public void onBtnLeftClick() {
                appAction.activateCoupon(couponBO.getId(), new CallbackListener<Boolean>() {
                    @Override
                    public void onSuccess(Boolean data) {
                        if (data) {
                            ToastUtil.showShort(context, "已发布");
                            fragment.reloadData();
                        } else {
                            ToastUtil.showShort(context, "发布失败");
                        }
                        dialog.dismiss();
                    }

                    @Override
                    public void onFailure(String errorCode, String message) {
                        ToastUtil.showShort(context, message);
                        dialog.dismiss();
                    }
                });
            }
        });

        dialog.setOnBtnRightClickL(new OnBtnRightClickL() {
            @Override
            public void onBtnRightClick() {
                dialog.dismiss();
            }
        });
    }

    private void confirmUnderCarriage(final CustomerCouponBO couponBO) {
        final NormalDialog dialog = new NormalDialog(context);
        dialog.content("是否确定撤回？")
                .contentTextColor(context.getResources().getColor(R.color.text_blue))
                .isTitleShow(false)
                .showAnim(bas_in)
                .dismissAnim(bas_out)
                .show();

        dialog.setOnBtnLeftClickL(new OnBtnLeftClickL() {
            @Override
            public void onBtnLeftClick() {
                appAction.inactivateCoupon(couponBO.getId(), new CallbackListener<Boolean>() {
                    @Override
                    public void onSuccess(Boolean data) {
                        if (data) {
                            ToastUtil.showShort(context, "已撤回");
                            fragment.reloadData();
                        } else {
                            ToastUtil.showShort(context, "撤回失败");
                        }
                        dialog.dismiss();
                    }

                    @Override
                    public void onFailure(String errorCode, String message) {
                        ToastUtil.showShort(context, message);
                        dialog.dismiss();
                    }
                });
                dialog.dismiss();
            }
        });

        dialog.setOnBtnRightClickL(new OnBtnRightClickL() {
            @Override
            public void onBtnRightClick() {
                dialog.dismiss();
            }
        });
    }
}
